<?php
namespace app\app\controller;

use app\common\controller\App;
use think\Db;
use think\Exception;


class Jianyi extends App
{


    public function getList()
    {

        $uid=$this->getLoginUid();
        $list=Db::name("Jianyi")
            ->where(['uid'=>$uid])
            ->order("id desc")
            ->select();
        if (!empty($list)){
            foreach ($list as $key=>$value){
                $list[$key]['content']=trimAll($value['content']);
            }
        }

        return $this->ok('请求成功',$list);

    }

    //我的建议详情
    public function detail()
    {
        $id=request()->param("id",'0','intval');
        $uid=$this->getLoginUid();
        $detail=Db::name("Jianyi")->where(['uid'=>$uid,'id'=>$id])->find();
        $status=model("Jianyi")::STATUS;
        if (empty($detail)){
            return $this->fail('建议不存在');
        }else{
            $detail['create_time']=date("Y年m月d日",$detail['create_time']);
            $detail['images']=model("File")->getPaths($detail['images']);
            $detail['status_title']=isset($status[$detail['status']])?$status[$detail['status']]:'';
            return $this->ok('请求成功',$detail);
        }
    }

    //删除
    public function del()
    {
        $id=request()->param("id",'0','intval');
        $uid=$this->getLoginUid();
        $res=model("Jianyi")->where(['uid'=>$uid,'id'=>$id])->delete();
        if ($res){
            return $this->ok('删除成功');
        }else{
            return $this->fail('删除失败');
        }
    }

    //提交建议
    public function add()
    {

        $uid=$this->getLoginUid();;
        $data['images']=request()->param("images");
        $data['content']=request()->param("content");
        $data['is_niming']=request()->param("is_niming");
        $user_info=model("Member")->where(['uid'=>$uid])->find();
        $data['house_id']=$user_info['house_id'];
        $data['room_id']=$user_info['sushe_id'];
        if (empty($data['content'])){
            return $this->fail('请填写提交内容');
        }
        $data['uid']=$uid;
        $data['create_time']=time();
        $data['images']=is_array($data['images'])?implode(',',$data['images']):$data['images'];

        $my_tousu=model("Jianyi")->where(['uid'=>$uid,'status'=>0])->count();

        if ($my_tousu>2){
            return $this->fail('您提交的次数过多请歇会');

        }

        $res=model("Jianyi")->insert($data);
        if ($res){
            $content='感谢您的建议,已提交成功请等待宿管处理';
            model("Message")->add_message($content,$data['uid']);
            return $this->ok('提交成功');
        }else{
            return $this->fail('提交失败');
        }
    }




}
